import 'package:flutter/material.dart';
import 'package:leader_doctor_flutter/common/utils/string_utils.dart';

import '../data/app.dart';

/// @Author lixiaobin
/// @Date 2023/11/13
/// @Description
class ImageLoader {
  /// 加载患者头像
  static Widget loadPatientAvatar(String? src, {String? sex, Size? size}) {
    var defImage = 'assets/images/icon_patient_def.webp';
    if (sex == '1' || sex == '男') {
      defImage = 'assets/images/icon_patient_man.webp';
    } else if (sex == '0' || sex == '女') {
      defImage = 'assets/images/icon_patient_women.webp';
    }
    return loadImage(src, size: size, errorImg: defImage);
  }

  /// 加载医生自己头像
  static Widget loadSelfAvatar(String? src, {String? sex, Size? size, BoxFit? fit}) {
    var defImage = 'assets/images/icon_doctor_def.webp';
    if (sex == '1' || sex == '男') {
      defImage = 'assets/images/icon_self_doctor_man.webp';
    } else if (sex == '0' || sex == '女') {
      defImage = 'assets/images/icon_self_doctor_women.webp';
    }
    return loadImage(src, size: size, errorImg: defImage, fit: fit);
  }

  /// 加载医生头像
  static Widget loadDoctorAvatar(String? src, {String? sex, Size? size}) {
    var defImage = 'assets/images/icon_doctor_def.webp';
    if (sex == '1' || sex == '男') {
      defImage = 'assets/images/icon_doctor_man.webp';
    } else if (sex == '0' || sex == '女') {
      defImage = 'assets/images/icon_doctor_women.webp';
    }
    return loadImage(src, size: size, errorImg: defImage);
  }

  /// 加载网络图片
  static Widget loadImage(String? src, {Size? size, String? errorImg, Widget? errorWidget, BoxFit? fit}) {
    var error = Image.asset(
      errorImg ?? '',
      width: size?.width,
      height: size?.height,
      fit: fit,
    );
    if (src == null || src.isEmpty) {
      if (errorWidget != null) return errorWidget;
      if (StringUtils.isNotEmpty(errorImg)) return error;
      return Container();
    }
    var image = Image.network(src, width: size?.width, height: size?.height, fit: fit,
        errorBuilder: (context, child, loadingProgress) {
      return StringUtils.isNotEmpty(errorImg) ? error : Container();
    }, loadingBuilder: (context, child, loadingProgress) {
      var loaded = loadingProgress?.expectedTotalBytes == loadingProgress?.cumulativeBytesLoaded;
      return loaded ? child : (StringUtils.isNotEmpty(errorImg) ? error : Container());
    });
    return image;
  }

  /// 加载本地图片
  static Widget loadAsset(String src, double s, {Size? size, Color? color, BoxFit? fit, Key? key}) {
    return Image.asset(
      key: key,
      'assets/images/$src',
      width: size?.width ?? s,
      height: size?.height ?? s,
      color: color,
      fit: fit,
    );
  }
}
